<template>
  <!-- 面包屑导航栏 -->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>订单管理</el-breadcrumb-item>
    <el-breadcrumb-item>订单列表</el-breadcrumb-item>
  </el-breadcrumb>

  <!-- 卡片视图区域 -->
  <el-card>
    <el-row>
      <el-col :span="8">
        <el-input placeholder="请输入内容">
          <template #append>
            <el-button icon="el-icon-search"></el-button>
          </template>
        </el-input>
      </el-col>
    </el-row>

    <!-- 订单列表数据 -->
    <el-table :data="orderlist" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column label="订单编号" prop="order_number"></el-table-column>
      <el-table-column label="订单价格" prop="order_price"></el-table-column>
      <el-table-column label="是否付款" prop="pay_status">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.pay_status === '1'"
            >已付款</el-tag
          >
          <el-tag type="danger" v-else>未付款</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否发货" prop="is_send">
        <template v-slot="scope">
          {{ scope.row.is_send }}
        </template>
      </el-table-column>
      <el-table-column label="下单时间" prop="create_time">
        <template v-slot="scope">
          {{ dateFormat(scope.row.create_time) }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-edit"
          @click="showBox"
        ></el-button>
        <el-button
          size="mini"
          type="success"
          icon="el-icon-location"
          @click="showProgressBox"
        ></el-button>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 展示物流进度的对话框 -->
    <el-dialog title="物流进度" v-model="progressVisible" width="50%">
      <!-- 时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </el-card>

  <!-- 修改地址的对话框 -->
  <el-dialog
    title="修改地址"
    v-model="addressVisible"
    width="50%"
    @close="addressDialogClosed"
  >
    <el-form
      :model="addressForm"
      :rules="addressFormRules"
      ref="addressFormRef"
      label-width="100px"
    >
      <el-form-item label="省市区/县" prop="address1">
        <el-cascader
          :options="cityData"
          v-model="addressForm.address1"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="详细地址" prop="address2">
        <el-input v-model="addressForm.address2"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false"
          >确 定</el-button
        >
      </span>
    </template>
  </el-dialog>
</template>

<script>
import {
  computed,
  getCurrentInstance,
  onMounted,
  reactive,
  ref,
  toRefs,
  inject,
} from "vue";
import cityData from "./citydata.js";
export default {
  setup() {
    // 获取vue实例
    const { proxy } = getCurrentInstance();
    // 注入一个axios
    const axios = inject("axios");

    // 请求参数
    const queryInfo = reactive({
      query: "",
      pagenum: 1,
      pagesize: 10,
    });

    // 全部数据
    let total = ref(0);

    // 存放渲染数据
    const order = reactive({
      orderlist: [], // 全部数据
      addressForm: {
        // 地址表单数据
        address1: [], // 省市区/县
        address2: "", // 详细地址
      },
      progressInfo: [], // 存储进度信息
    });

    // 表单预验证
    const addressFormRules = {
      address1: [
        { required: true, message: "请选择省市区县", trigger: "blur" },
      ],
      address2: [
        { required: true, message: "请填写详细地址", trigger: "blur" },
      ],
    };

    // 修改地址显示与隐藏
    let addressVisible = ref(false);

    // 物流进度显示与隐藏
    let progressVisible = ref(false);

    // 获取dom元素
    let addressFormRef = ref(null);

    // 页面初始化请求数据
    const getOrderList = async () => {
      const { data: res } = await axios.get("orders", {
        params: queryInfo,
      });
      if (res.meta.status !== 200)
        return proxy.$message.error("获取订单列表失败！");
      order.orderlist = res.data.goods;
      total.value = res.data.total;
    };
    onMounted(() => {
      getOrderList();
    });

    // size-change
    const handleSizeChange = (newSize) => {
      queryInfo.pagesize = newSize;
      getOrderList();
    };
    // @current-change
    const handleCurrentChange = (newPage) => {
      queryInfo.pagenum = newPage;
      getOrderList();
    };

    // 展示修改地址的对话框
    const showBox = () => {
      addressVisible.value = true;
    };

    // 监听修改地址的对话框关闭事件
    const addressDialogClosed = () => {
      addressFormRef.value.resetFields();
    };

    // 展示物流进度的对话框
    const showProgressBox = async () => {
      // const { data: res } = await axios.get("/kuaidi/1106975712662");

      // if (res.meta.status !== 200) {
      //   return proxy.$message.error("获取物流进度失败！");
      // }

      // 由于测试接口有问题，这里手写一个假数据用于渲染
      const res = {
        data: [
          {
            time: "2018-05-10 09:39:00",
            ftime: "2018-05-10 09:39:00",
            context: "已签收,感谢使用顺丰,期待再次为您服务",
            location: "",
          },
          {
            time: "2018-05-10 08:23:00",
            ftime: "2018-05-10 08:23:00",
            context:
              "[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件",
            location: "",
          },
          {
            time: "2018-05-10 07:32:00",
            ftime: "2018-05-10 07:32:00",
            context: "快件到达 [北京海淀育新小区营业点]",
            location: "",
          },
          {
            time: "2018-05-10 02:03:00",
            ftime: "2018-05-10 02:03:00",
            context:
              "快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]",
            location: "",
          },
          {
            time: "2018-05-09 23:05:00",
            ftime: "2018-05-09 23:05:00",
            context: "快件到达 [北京顺义集散中心]",
            location: "",
          },
          {
            time: "2018-05-09 21:21:00",
            ftime: "2018-05-09 21:21:00",
            context: "快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]",
            location: "",
          },
          {
            time: "2018-05-09 13:07:00",
            ftime: "2018-05-09 13:07:00",
            context: "顺丰速运 已收取快件",
            location: "",
          },
          {
            time: "2018-05-09 12:25:03",
            ftime: "2018-05-09 12:25:03",
            context: "卖家发货",
            location: "",
          },
          {
            time: "2018-05-09 12:22:24",
            ftime: "2018-05-09 12:22:24",
            context: "您的订单将由HLA（北京海淀区清河中街店）门店安排发货。",
            location: "",
          },
          {
            time: "2018-05-08 21:36:04",
            ftime: "2018-05-08 21:36:04",
            context: "商品已经下单",
            location: "",
          },
        ],
        meta: { status: 200, message: "获取物流信息成功！" },
      };
      order.progressInfo = res.data;

      progressVisible.value = true;
    };

    // 时间格式化
    const dateFormat = computed(() => {
      return function (originVal) {
        const dt = new Date(originVal);

        const y = dt.getFullYear();
        const m = (dt.getMonth() + 1 + "").padStart(2, "0");
        const d = (dt.getDate() + "").padStart(2, "0");

        const hh = (dt.getHours() + "").padStart(2, "0");
        const mm = (dt.getMinutes() + "").padStart(2, "0");
        const ss = (dt.getSeconds() + "").padStart(2, "0");

        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
      };
    });

    return {
      queryInfo,
      total,
      cityData,
      ...toRefs(order),
      addressFormRules,
      addressVisible,
      addressFormRef,
      progressVisible,
      handleSizeChange,
      handleCurrentChange,
      showBox,
      addressDialogClosed,
      showProgressBox,
      dateFormat,
    };
  },
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>